<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="产品入库单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serial">
              <a-input v-model="model.serial" placeholder="请输入产品入库单号" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="documentDate">
              <j-date placeholder="请选择单据日期" v-model="model.documentDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品入库类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inputType">
              <j-dict-select-tag
                type="list"
                v-model="model.inputType"
                dictCode="hf_product_input_type"
                placeholder="请选择产品入库类型"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="红蓝单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRed">
              <j-dict-select-tag type="list" v-model="model.isRed" dictCode="hf_if_red" placeholder="请选择红蓝单" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="交货单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <j-dict-select-tag
                v-model="model.inputDepartment"
                placeholder="请输入交货单位"
                dict-code="sys_depart,depart_name,id"
              ></j-dict-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="制单" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operator">
              <a-input v-model="model.operator" placeholder="请输入制单" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checker">
              <a-input v-model="model.checker" placeholder="请输入审核员" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审核时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkTime">
              <a-input v-model="model.checkTime" placeholder="请输入审核时间" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import store from '@/store'
import dayjs from 'dayjs'

export default {
  name: 'HfProductionProductInputModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {
        operator: store.getters.userInfo.realname,
        documentDate: dayjs().format('YYYY-MM-DD'),
        inputType: '1',
        isRed: 0,
        status: 0,
        isClosed: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        documentDate: [{ required: true, message: '请输入单据日期!' }],
        pickingType: [{ required: true, message: '请输入领料类型!' }]
      },
      url: {
        add: '/production/hfProductionProductInput/add',
        edit: '/production/hfProductionProductInput/edit'
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>
